package com.gzlg.hospital.controller;

import com.gzlg.hospital.pojo.Admin;
import com.gzlg.hospital.pojo.Doctor;
import com.gzlg.hospital.pojo.Patient;
import com.gzlg.hospital.service.AdminService;
import com.gzlg.hospital.service.DoctorService;
import com.gzlg.hospital.service.PatientService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller

public class LoginController {
    @Autowired
    private PatientService patientService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private DoctorService doctorService;
    @GetMapping("/login")
    public String  gotoLogin() {
        return "login";
    }


    @PostMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("pwd") String password,
                        @RequestParam("role") String role,
                        RedirectAttributes redirectAttributes,
                        HttpSession session) {

        Object user = null;


        switch (role) {
            case "ADMIN":
                Admin admin = adminService.adminlogin(account);
                if (admin != null && admin.getPwd().equals(password)) {
                    user = admin;
                    session.setAttribute("user", user);
                    return "redirect:/admin/index";
                }
                break;

            case "DOCTOR":
                Doctor doctor = doctorService.getDoctorByAccount(account);
                if (doctor != null && doctor.getPwd().equals(password)) {
                    user = doctor;
                    session.setAttribute("user", user);
                    return "redirect:/DoctorIndex";
                }
                break;

            case "PATIENT":
                Patient patient = patientService.getPatientByAccount(account);
                if (patient != null && patient.getPwd().equals(password)) {
                    user = patient;
                    session.setAttribute("user", user);
                    return "redirect:/PatientIndex";
                }
                break;

            default:
                redirectAttributes.addFlashAttribute("message", "无效的角色");
                return "redirect:/login";
        }

        // 如果未找到匹配用户
        redirectAttributes.addFlashAttribute("message", "账户或密码错误");
        return "redirect:/login";
    }

    @PostMapping("/logout")
    public String logout(HttpServletRequest request) {
        // 获取当前 session（如果存在）
        HttpSession session = request.getSession(false);

        if (session != null) {
            // 使 session 失效
            session.invalidate();
        }

        return "redirect:/login";
    }


}
